package cn.kailangtu.dataspot.activify;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import cn.kailangtu.commonevents.eventEntity.KeyboardLockerActionEvent;
import cn.kailangtu.commonevents.eventEntity.KeyboardLockerResultEvent;
import cn.kailangtu.dataspot.data.consts.KeyboardAction;
import cn.kailangtu.dataspot.data.context.KeyboardLockerContext;
import cn.kailangtu.dataspot.databinding.FragmentKeyboardLockerBinding;

public class KeyboardLockerFragment extends KeyboardBaseFragment {

    private FragmentKeyboardLockerBinding binding;

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentKeyboardLockerBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initListener();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    private void initListener(){
        binding.btnConfirmCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new KeyboardLockerResultEvent(false,""));
                reset();
            }
        });
        binding.btnConfirmSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newPwd = binding.tvConfirmPwd1.getText().toString()
                        + binding.tvConfirmPwd2.getText().toString()
                        + binding.tvConfirmPwd3.getText().toString()
                        + binding.tvConfirmPwd4.getText().toString();

                EventBus.getDefault().post(new KeyboardLockerResultEvent(true,newPwd));
                reset();
            }
        });
        binding.tvConfirmPwd1.setOnClickListener(keybaodListener);
        binding.tvConfirmPwd2.setOnClickListener(keybaodListener);
        binding.tvConfirmPwd3.setOnClickListener(keybaodListener);
        binding.tvConfirmPwd4.setOnClickListener(keybaodListener);
    }

    private View.OnClickListener keybaodListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TextView tv = (TextView)view;
            int currValue = Integer.parseInt(tv.getText().toString());
            currValue++;
            if(currValue > 9)
            {
                currValue = 0;
            }
            tv.setText(String.valueOf(currValue));

        }
    };




    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(KeyboardLockerActionEvent event){
        keyboard(event.getActionValue());
    }

    private void keyboard(int action){
        switch(action) {
            case KeyboardAction.OPEN:
                reset();
                break;
            case KeyboardAction.ENTRY:
                if(KeyboardLockerContext.getSelectedView() != null)
                {
                    KeyboardLockerContext.getSelectedView().callOnClick();
                }
                break;
            case KeyboardAction.LEFT:
                Log.d("DDDDDDDDDDDDD","LEFT:进来了");
                if(KeyboardLockerContext.getSelectedView() != null)
                {
                    KeyboardLockerContext.setSelectedView(binding.tvConfirmPwd1);
                }
                else if(binding.tvConfirmPwd1.equals( KeyboardLockerContext.getSelectedView()))
                {
                    KeyboardLockerContext.setSelectedView(binding.btnConfirmSave);
                }
                else if( binding.btnConfirmSave.equals(KeyboardLockerContext.getSelectedView()))
                {
                    KeyboardLockerContext.setSelectedView(binding.btnConfirmCancel);
                }
                else if( binding.btnConfirmCancel.equals( KeyboardLockerContext.getSelectedView()))
                {
                    KeyboardLockerContext.setSelectedView(binding.tvConfirmPwd4);
                }
                else if(binding.tvConfirmPwd4.equals(KeyboardLockerContext.getSelectedView() ))
                {
                    KeyboardLockerContext.setSelectedView(binding.tvConfirmPwd3);
                }
                else if(binding.tvConfirmPwd3.equals( KeyboardLockerContext.getSelectedView()))
                {
                    KeyboardLockerContext.setSelectedView(binding.tvConfirmPwd2);
                }
                else
                {
                    KeyboardLockerContext.setSelectedView(binding.tvConfirmPwd1);
                }


                break;
            case KeyboardAction.RIGHT:

                if( binding.tvConfirmPwd1.equals(KeyboardLockerContext.getSelectedView()))
                {
                    KeyboardLockerContext.setSelectedView(binding.tvConfirmPwd2);
                }
                else if( binding.btnConfirmSave.equals( KeyboardLockerContext.getSelectedView()))
                {
                    KeyboardLockerContext.setSelectedView(binding.tvConfirmPwd1);
                }
                else if( binding.btnConfirmCancel.equals(KeyboardLockerContext.getSelectedView()))
                {
                    KeyboardLockerContext.setSelectedView(binding.btnConfirmSave);
                }
                else if( binding.tvConfirmPwd4.equals( KeyboardLockerContext.getSelectedView()))
                {
                    KeyboardLockerContext.setSelectedView(binding.btnConfirmCancel);
                }
                else if( binding.tvConfirmPwd3.equals(KeyboardLockerContext.getSelectedView()))
                {
                    KeyboardLockerContext.setSelectedView(binding.tvConfirmPwd4);
                }
                else if( binding.tvConfirmPwd2.equals(KeyboardLockerContext.getSelectedView()))
                {
                    KeyboardLockerContext.setSelectedView(binding.tvConfirmPwd3);
                }
                else
                {
                    KeyboardLockerContext.setSelectedView(binding.tvConfirmPwd1);
                }


                break;
        }
    }

    public void reset()
    {
        binding.tvConfirmPwd1.setText("0");
        binding.tvConfirmPwd2.setText("0");
        binding.tvConfirmPwd3.setText("0");
        binding.tvConfirmPwd4.setText("0");
    }
}
